package com.broadcaster.broadcaster.core;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.lang.reflect.Method;


@Slf4j
@Aspect
public  class BroadcasterSpect {


    public static Integer verson=0;

    private static final String pointCut = "@annotation(com.broadcaster.broadcaster.core.Broadcaster)";
    @Autowired
    private RedisTemplate redisTemplate;
    @Pointcut(pointCut)
    public void logPointCut() {
    }
    @Before("logPointCut()")
    public void doAfterReturning(JoinPoint joinPoint) {
       String className=null;
       String methodName=null;
       BroadcastServices broadcastServices= getBroadcastServices(joinPoint);
       if(broadcastServices!=null){
           try {
               className = joinPoint.getTarget().getClass().getName();
               methodName = joinPoint.getSignature().getName();
               Broadcaster distributeMap=getAnnotationLog(joinPoint);
               if(!distributeMap.className().equals("1") && !distributeMap.methodName().equals("1")){
                   className=distributeMap.className();
                   methodName=distributeMap.methodName();
               }
               if(!BroadcasterServer.versionMap.containsKey(className+ methodName)){
                   redisTemplate.convertAndSend("map", className+","+methodName);
               }else {

               }
               if(BroadcasterServer.versionMap.containsKey(className+methodName)){
                   BroadcasterServer.versionMap.remove(className+methodName);
               }

           }catch (Exception E){
               E.printStackTrace();

           }
       }else {

       }


    }

    @AfterThrowing(value = "logPointCut()", throwing = "e")
    public void doAfterThrowing(JoinPoint joinPoint, Exception e) {


    }

    private Broadcaster getAnnotationLog(JoinPoint joinPoint) throws Exception {
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();
        Broadcaster classAnnotation =method.getAnnotation(Broadcaster.class);
        if (method != null) {
            return classAnnotation;
        }
        return null;
    }
    private   Consumer getConsumer(JoinPoint joinPoint) {
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();
        Consumer classAnnotation =method.getAnnotation(Consumer.class);
        if (method != null) {
            return classAnnotation;
        }
        return null;
    }

    private   BroadcastServices getBroadcastServices(JoinPoint joinPoint) {
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();
        BroadcastServices classAnnotation =method.getAnnotation(BroadcastServices.class);
        if (method != null) {
            return classAnnotation;
        }
        return null;
    }
}
